<?php
/**
 * spsNewsletter sendNewsletter Model
 * 
 * @author Satyan Shah <joomlaDesign@modernbridge.com>
 * @link http://www.modernbridge.com
 * @license GNU/GPL
 */
 
 
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.application.component.model');


class spsNewslettersModelsendNewsLetter extends JModel
{
	function __construct()
	{
		parent::__construct();
		$this->set_current_newsletter();
	}

	function set_current_newsletter()
	{
		// get the currently selected newsletter to send (consume post data)
		$current_select_val=JRequest::getVar( 'newsletterToSend','', 'post', 'string');
		// if the select box hasn't been used yet then current_select_val has not been set, provide a default value for it.
		if(!$current_select_val) {
			$available_newsletters=$this->getAllNewsletters();
			$current_select_val=$available_newsletters[0]->id;
		}
    
		// set the id of the current newsletter 
		$this->setId((int)$current_select_val); 

	}
  
	// functon to covert image tags from relative to absolute paths
	// TODO: This breaks if your image is already an aboslute path
	function convertImgTags($html_content)
	{ 
		global $mainframe;
		$mod_html_content=null;
		// get the URL for our site so we can build absolute URLs
		$imgPathPrefix=$mainframe->getSiteURL();
		// define the search tag to search for
		$imgTag='<img src="'.$imgPathPrefix;
		// store the modified html content in a local variable
		$mod_html_content=str_replace('<img src="',$imgTag,$html_content);
		// return the modified content, image tags are now absolute for use in the newslettter
		return $mod_html_content;
	}
	
	// function to convert the images used in a background tag
	// TODO: I think this breaks if your background in a table is a color
	function convertBackgroundTags($html_content)
	{ 
		global $mainframe;
		$mod_html_content=null;
		// get the URL for our site so we can build absolute URLs
		$imgPathPrefix=$mainframe->getSiteURL();
		// define the search tag to search for
		$imgTag='background="'.$imgPathPrefix;
		// store the modified html content in a local variable
		$mod_html_content=str_replace('background="',$imgTag,$html_content);
		// return the modified content, image tags are now absolute for use in the newslettter
		return $mod_html_content;
	}
	
	// function to create the "unsubscribe link"
	function convertUnsubscribeLink($html_content,$subscriberID)
	{
		global $mainframe;
		$mod_html_content=null;
		$unsubscribeString='<a href="'.$mainframe->getSiteURL().'?option=com_spsnewsletter&view=remove&task=removeSubscriber&subscriberID='.$subscriberID.'">[unsubscribe]</a>';
		$mod_html_content=str_replace('[unsubscribe link]',$unsubscribeString,$html_content);
		return $mod_html_content;
	  
	}

	function getAllNewsletters() 
	{
		$db =& JFactory::getDBO();
		$query = 'SELECT * FROM #__spsnewsletter_newsletters';
		$db->setQuery( $query );
		$available_newsletters = $db->loadObjectList();
		$this->availableNewsletters=$available_newsletters;
		return $available_newsletters;
	}

	function getComponentParameters()
	{
		// get parameters for the form 
		$params = &JComponentHelper::getParams( 'com_spsnewsletter' );
		return $params; 
	}

	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}


	function &getData()
	{
   		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__spsnewsletter_newsletters '.
					'  WHERE id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->name = null;
			$this->_data->body = null;
		}
		
		return $this->_data;
	}
}
?>
